<!-- 我的处方页面 -->

<template>
	<view class="container">
		<lk-topbar :title="'我的处方'" :backcolor="'#fff'" @back="back"></lk-topbar>
		<view class="p30">
			<!-- 引入记录组件 -->
			<lk-record :types="'pres'" @delectTimeTap='delectTimeTap'  @delectTap="delectTap" :lists='prescription' @change="change"></lk-record>
		</view>
		
		<view class="add" @click="add"> <image class="editImg" src="../../static/images/add.png" mode=""></image>添加</view>
		<view class="btn w75 h84 radius50" @click="save()"> 保存</view>
		
	</view>
</template>

<script>
	import lkRecord from "../../components/lk-record/index.vue"
	export default {
		name:"",
		data() {
			return {
				prescription:[],//我的处方
				isSave:true,
			}
		},
		props:{
			
		},
		components:{
			lkRecord
		},
		onLoad() {
			this.getUserDetail();
		},
		onShow() {
			let pages =getCurrentPages();
			let cur = pages[pages.length -1];
			if(typeof cur.data.med != 'undefined'){
				this.isSave = false;
				if(cur.data.cindex != -1){
					this.prescription[cur.data.pindex].med.splice(cur.data.cindex,1,cur.data.med);
				}else{
					this.prescription[cur.data.pindex].med.push(cur.data.med)
				}
			}
		},
		methods: {
			// 添加时间段
			add(){
				this.isSave = false;
				this.prescription.push({
					time:"",
					med:[]
				})
			},
			// 保存
			save(){
				let _this = this;
					console.log(_this.prescription);
					for(let i =0; i<_this.prescription.length;i++){
						if(_this.prescription[i].time == ''|| _this.prescription[i].med.length<1){
							wx.showToast({
								title:"请填写完整信息",
								icon:"error"
							})
							return false;
						}
					}
				
				 _this.$utils.request({
					 url:"/patient/user_info/",
					 method:"PUT",
					 data:{
						 prescription:_this.prescription,
					 }
				 }).then(res=>{
					 wx.showToast({
						 title:"保存成功！"
					 })
					 this.isSave =true;
					 setTimeout(()=>{
						this.$routeJump.back()
					 },2000)
				 })
			},
			//请求用户信息
			getUserDetail(){
				let _this = this;
				 _this.$utils.request({
					 url:"/patient/user_info/",
				 }).then(res=>{
					 _this.prescription = res.data.prescription;
				 })
			},
			/** 更改时间
			 * @param {Object} e 
			 */
			change(e){
				this.prescription[e.i].time=e.time
				this.isSave = false;
			},
			
			/**
			 * 删除某个时段
			 * @param {Object} o
			 */
			delectTimeTap(o){
				this.isSave  =false;
				this.prescription.splice(o,1);
			},
			
			/**
			 * 删除某条数据
			 * @param {Object} o 返回数据
			 */
			delectTap(o){
				this.isSave  =false;
				this.prescription[o.index].med.splice(o.ind,1);
			},
			
			// 页面返回
			back(){
				let _this =this;
				if(_this.isSave){
					_this.$routeJump.back()
				}else{
					wx.showModal({
						title:"提示",
						content:"你还没保存，是否保存？",
						success(res){
							if(res.confirm){
								_this.save()
							}else{
								_this.$routeJump.back()
							}
						}
					})
				}
			},
		}
	}
	
</script>

<style lang="scss" scoped>
	.container{
		.add{
			width: 90%;
			height: 76rpx;
			margin: 0 auto 40rpx;
			font-size: 32rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border: 1rpx solid #ccc;
		}
		.btn{
			background-color: #ffb526;
			margin: 80rpx auto 40rpx;
		}
	}
	
</style>
